package com.chengdu.third.controller.admin;

import com.chengdu.third.pojo.Basic;
import com.chengdu.third.pojo.User;
import com.chengdu.third.service.IBasicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

import java.util.Date;
import java.util.List;

/**
 * @author jingbo
 * @create 2021--08--19  16:05
 */

@Controller
@RequestMapping("/admin")
public class BasicController {

    @Autowired
    private IBasicService iBasicService;


    //查询所有分类的方法
    @GetMapping("/getBasicshow")
    public String getUserShow(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        //规定第几页，每页多少个
//        pagenum是统计页数的
        PageHelper.startPage(pagenum, 5);
        List<Basic> allBasic = iBasicService.findAll();
        //得到分页结果对象
        PageInfo<Basic> pageInfo = new PageInfo<>(allBasic);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/Basic_list";
    }

    //跳转新增用户的页面，放一个空的User到model中
    @GetMapping("/getBasicshow/input")
    public String toAddBlog(Model model){
        model.addAttribute("basic", new Basic());  //返回一个blog对象给前端th:object
        return "admin/Basic_add.html";
    }

    @PostMapping("/getBasicshow")
    public String addUsershow(Basic basic, HttpSession session, RedirectAttributes attributes){
        basic.setReviw_units("政府");
        iBasicService.addBasic(basic.getArea_prefectures(),basic.getArea_town(),basic.getArea_village(),basic.getContext(),basic.getReviw_units());
        attributes.addFlashAttribute("msg", "添加成功");
        return "redirect:/admin/getBasicshow";
    }

    //    删除Usershow
    @GetMapping("/BasicDelete/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        iBasicService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getBasicshow";
    }

    //    修改Usershow,转移到修改页面
    @GetMapping("/BasicUpdate/{id}/update")
    public String updateUser(@PathVariable Long id, Model model){
        model.addAttribute("basic", iBasicService.getBasicByid(id));
        return "admin/Basic-update";
    }

    //真正修改提交的页面
    @PostMapping("/Basicupdate/{id}")
    public String editTag(@PathVariable Long id, Basic basic, RedirectAttributes attributes){  //修改
        iBasicService.updateByid(basic.getArea_prefectures(),basic.getArea_town(),basic.getArea_village(),basic.getContext(),id);
        attributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/admin/getBasicshow";
    }

}
